<?php
namespace app\admin\controller;
use app\admin\inter_logic\CarLogic;
use app\admin\inter_logic\DriverLogic;
use app\admin\logic\LogLogic;
use app\inter\logic\DriverLogic as InterDriverLogic;
use my\Security;
use think\Db;
use think\Env;

class DriverLoginLog extends Base
{

    protected $map = array();
    public function _initialize()
    {
        parent::_initialize();
        if($this->base_condition){
            switch($this->base_condition['admin_type']){
                case 2:
                    $this->map['work_province'] = $this->base_condition['region_area'];
                    break;
                case 3:
                    $this->map['work_city'] = $this->base_condition['region_area'];
                    break;
                case 4:
                    $this->map['work_county'] = $this->base_condition['region_area'];
                    break;
            }
        }
    }

	public function index(){
        $config['url'] = url('driver_login_log/table');
        $config['local'] = Env::get('coordinate'.ENV.'_loginAdd');
        $this->assign($config);
        return view();
	}

	public function table()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $logic = new LogLogic();
        $res = $logic->getInnerDriverLoginLog($this->map, 'lld.*,d.driver_name',true,$page, $limit);
        $count = Db::table('xm_login_log_driver lld')
            ->join('driver_base d','d.driver_id=lld.driver_id')
            ->join('driver_info i','d.driver_id = i.driver_id')->where($this->map)->count();
        $res = decode_array_2($res);
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

	//搜索
    public function search()
    {
        $page = input('get.page');
        $limit = input('get.limit');
        $start = strtotime(input('get.start'));
        $end = strtotime(input('get.end'));
        $phone = trim(input('get.driver_phone'),' ');
        $map = $this->map;
        $phone && $map['d.driver_phone'] = Security::encrypt($phone);
        input('get.start')&&$map['login_time'] = array('egt',$start);
        input('get.end')&&$map['login_time'] = array('elt',$end);
        input('get.start')&&input('get.end')&&$map['login_time'] = array('between',array($start,$end));
        $logic = new LogLogic();
        $res = $logic->getInnerDriverLoginLog($map, 'lld.*,driver_name',true,$page, $limit);
        $count = Db::table('xm_login_log_driver lld')
            ->join('driver_base d','d.driver_id=lld.driver_id')
            ->join('driver_info i','d.driver_id = i.driver_id')->where($map)->count();
        $res = decode_array_2($res);
        echo json_encode(array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $res
        ));
    }

    //登录位置---转为xmcx服务器使用20180925
    public function local()
    {
        $center = trim(input('get.center'));
        $center = explode(',',$center);
        if($center&&count($center)==2){
            $config['local'] = json_encode($center);
            $config['protocol'] = PROTOCOL;
            $this->assign($config);
            return view();
        }
    }
}